package com.arpa.ntocc.basic.api.controller;

import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.basic.api.domain.dto.AppInfoDTO;
import com.arpa.ntocc.basic.api.domain.entity.AppInfo;
import com.arpa.ntocc.basic.api.domain.vo.AppInfoVO;
import com.arpa.ntocc.basic.api.service.IAppInfoService;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.ntocc.common.security.annotation.NoReSubmit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * APP发布信息 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-03-17
 */
@Api(tags = "APP发布信息")
@RestController
@RequestMapping(value = "/appInfo", produces = "application/json;charset=UTF-8")
@Log4j2
public class AppInfoController extends BaseController {

    @Autowired
    private IAppInfoService iAppInfoService;

    /**
     * 获取 APP发布信息 列表
     *
     * @param appInfoDTO
     * @return
     */
    @ApiOperation(value = "获取 APP发布信息 列表", notes = "获取 APP发布信息 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = AppInfoVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(AppInfoDTO appInfoDTO) {
        try {
            AppInfoVO appInfoVOSum = iAppInfoService.queryListSum(appInfoDTO);
            List<AppInfoVO> appInfoVOList = iAppInfoService.queryList(appInfoDTO);
            Page returnPage = new Page(appInfoDTO.getPageNum(), appInfoDTO.getPageSize());
            returnPage.setRecords(appInfoVOList);
            returnPage.setTotal(appInfoVOSum.getTotal());
            returnPage.setSumObject(appInfoVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 APP发布信息
     *
     * @param appInfo
     * @return
     */
    @ApiOperation(value = "添加 APP发布信息 ", notes = "添加 APP发布信息 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @NoReSubmit
    @PostMapping()
    public Result create(AppInfo appInfo) {
        try {
            iAppInfoService.save(appInfo);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取APP发布信息详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取APP发布信息详情", notes = "获取APP发布信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "id", value = "APP发布信息id", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = AppInfo.class))
    @NeedLogin
    @GetMapping(value = "/{id}")
    public Result appInfo(@PathVariable String id) {
        try {
            AppInfo entity = iAppInfoService.getOne(new QueryWrapper<AppInfo>().lambda().eq(AppInfo::getId, id));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取最新APP版本
     *
     * @param type
     * @return
     */
    @ApiOperation(value = "获取最新APP版本信息", notes = "获取最新APP版本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "type", value = "类型", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = AppInfo.class))
    @GetMapping(value = "/new/{type}")
    public Result newAppInfo(@PathVariable String type) {
        try {
            AppInfo entity = iAppInfoService.getOne(new QueryWrapper<AppInfo>().lambda().eq(AppInfo::getType, type).orderByDesc(AppInfo::getGmtModified).last("LIMIT 1"));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除 APP发布信息
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除 APP发布信息 ", notes = "删除 APP发布信息 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "ids", value = "ids", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @NoReSubmit
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> ids) {
        try {
            iAppInfoService.remove(new QueryWrapper<AppInfo>().lambda().in(AppInfo::getId, ids));
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

}
